package com.example.demoamqp.send;

import cn.hutool.core.date.DateUtil;
import com.example.demoamqp.conatants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 死信队列
 *
 * @className: DeadSender
 * @projectName: demo-one
 * @auth: rosszhang
 * @date: 2023/12/28 16:49
 */
@Slf4j
@Component
public class DeadSender {
    private AmqpTemplate rabbitAmqpTemplate;

    @Autowired
    public void setRabbitAmqpTemplate(AmqpTemplate rabbitAmqpTemplate) {
        this.rabbitAmqpTemplate = rabbitAmqpTemplate;
    }

    /**
     * 死信队列
     *
     * @param msg
     * @param delayTime
     */
    public void sendDelay(String msg, int delayTime) {
        rabbitAmqpTemplate.convertAndSend(
                Constants.MQ_NORMAL_EXCHANGE,
                Constants.MQ_NORMAL_ROUTING_KEY,
                msg,
                process -> {
                    process.getMessageProperties().setExpiration(String.valueOf(delayTime));
                    return process;
                }
        );
        log.debug("[生产者：]发送消息:{},时间{},延迟{}秒", msg, DateUtil.date(), delayTime / 1000);

    }
}
